/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.com.educacionit.vehiculos.entidades;

import ar.com.educacionit.base.entidades.Persona;
import java.sql.*;
import java.util.*;

/**
 *
 * @author matias
 */
public class Vendedor extends Persona {
    private int cantAutosVendidos;

    public Vendedor() {

    }

    public int getCantAutosVendidos() {
        return cantAutosVendidos;
    }

    public void setCantAutosVendidos(int cantAutosVendidos) {
        this.cantAutosVendidos = cantAutosVendidos;
    }

    public void insertar(Connection conn) throws Exception{
        Statement stmtInsert= conn.createStatement();
        String sql= "INSERT INTO vendedores (ven_nombre, ven_apellido, ven_dni, ven_cant)"
                + "VALUES ('"+getNombre()+"', '"+getApellido()+"', '"+getNumeroDocumento()+"', "+getCantAutosVendidos()+")";
        System.out.println(sql);
        stmtInsert.execute(sql);
        stmtInsert.close();
    }

    public void modificar(Connection conn) throws Exception{
        Statement stmtUpdate= conn.createStatement();
        String sql= "UPDATE vendedores SET "
                + "ven_nombre= '"+getNombre()+"', "
                + "ven_apellido= '"+getApellido()+"', "
                + "ven_dni= '"+getNumeroDocumento()+"' ,"
                + "ven_cant= "+getCantAutosVendidos()
                + " WHERE ven_id= "+getId();
        stmtUpdate.execute(sql);
        stmtUpdate.close();
    }

    public void eliminar(Connection conn) throws Exception{
        Statement stmtDelete= conn.createStatement();
        String sql= "DELETE FROM vendedores WHERE ven_id= "+getId();
        stmtDelete.execute(sql);
        stmtDelete.close();
    }

    public ArrayList obtenerTodos(Connection conn) throws Exception{
        Statement stmtQuery= conn.createStatement();
        String sql= "SELECT * FROM vendedores";
        ResultSet rs= stmtQuery.executeQuery(sql);
        ArrayList vLista= new ArrayList();

        while(rs.next()){
            Vendedor c= new Vendedor();
            c.setId(rs.getInt("ven_id"));
            c.setNombre(rs.getString("ven_nombre"));
            c.setApellido(rs.getString("ven_apellido"));
            c.setNumeroDocumento(rs.getString("ven_dni"));
            c.setCantAutosVendidos(rs.getInt("ven_cant"));
            vLista.add(c);
        }
        return vLista;
    }

    @Override
    public String toString() {
        return getNombre()+" "+getApellido();
    }

}
